package fragments;


import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseFragment;
import com.example.administrator.newxlmedical.R;

import beans.NewUserInfoBean;
import beans.RegisterInfoBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import dialogs.SupplierRegisterInfoDialog;
import netframe.GetRetrofit;
import netframe.MyAction1;
import netframe.NetAddress;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.observables.GroupedObservable;
import rx.schedulers.Schedulers;
import singleton.SingletonClass;
import utils.ToastUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class SupplierRegisterFragment extends BaseFragment {


    @BindView(R.id.et_supplier_register_id_number)
    EditText etIdNumber;
    @BindView(R.id.tv_supplier_register_register)
    TextView tvRegister;
    Unbinder unbinder;
    @BindView(R.id.iv_supplier_rigister_back)
    ImageView ivBack;

    private RegisterInfoBean bean;
    private MyHandler allHandler;
    private int type;

    public SupplierRegisterFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_supplier_register, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected void initView() {
        super.initView();
        bean = (RegisterInfoBean) getArguments().getSerializable(Constants.SUPPLIER_REGISTER_FRAGMENT);
        allHandler = SingletonClass.getSingleton(MyHandler.class);
    }

    @OnClick({R.id.tv_supplier_register_register,R.id.iv_supplier_rigister_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_supplier_rigister_back :
                allHandler.sendEmptyMessage(HandlerConstant.REGISTER_SUCCESS_FRAGMENT_SURE);
                break;
            case R.id.tv_supplier_register_register :

                String idNumber = etIdNumber.getText().toString().trim();
                if (!TextUtils.isEmpty(idNumber)) {
                    searchIdNumber(idNumber);
                } else {
                    ToastUtil.showToast(mContext, "请输入身份证号!");
                }
                break;
        }
    }

    private void searchIdNumber(String idNumber) {
        GetRetrofit.getInstance().getUserInfo2(NetAddress.token, NetAddress.machineCode, "select * from UserInfo where IDCardCode = '" + idNumber + "'")
                .subscribeOn(Schedulers.io()).groupBy(new Func1<NewUserInfoBean, Boolean>() {
            @Override
            public Boolean call(NewUserInfoBean userInfoBean) {
                return userInfoBean == null ? false : userInfoBean.getData().size() > 0;
            }
        }).observeOn(AndroidSchedulers.mainThread()).flatMap(new Func1<GroupedObservable<Boolean, NewUserInfoBean>, Observable<NewUserInfoBean.DataBean>>() {
            @Override
            public Observable<NewUserInfoBean.DataBean> call(GroupedObservable<Boolean, NewUserInfoBean> booleanUserInfoBeanGroupedObservable) {
                if (booleanUserInfoBeanGroupedObservable.getKey()) {
                    return booleanUserInfoBeanGroupedObservable.subscribeOn(Schedulers.io()).flatMap(new Func1<NewUserInfoBean, Observable<NewUserInfoBean.DataBean>>() {
                        @Override
                        public Observable<NewUserInfoBean.DataBean> call(NewUserInfoBean userInfoBean) {
                            return Observable.from(userInfoBean.getData()).last();
                        }
                    });
                } else {
                    ToastUtil.showToast(mContext, "该身份证号尚未注册,请先在手持机上面注册");
                    return null;
                }
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<NewUserInfoBean.DataBean>() {
            @Override
            public void call(final NewUserInfoBean.DataBean dataBean) {
                bean.setIdNumber(dataBean.getIDCardCode());
                bean.setCardPositive(dataBean.getCardPositiveImg());
                bean.setCardOppositive(dataBean.getCardOppositeImg());
                bean.setCardQualification(dataBean.getQualificationsImg());
                bean.setCompany(dataBean.getSupplierName());
                bean.setName(dataBean.getName());
                showDialog();
            }
        }, new MyAction1());
    }

    private void showDialog() {
        new SupplierRegisterInfoDialog(mContext, R.style.dialog, bean, new SupplierRegisterInfoDialog.OnClickListener() {
            @Override
            public void onSureClickListener(SupplierRegisterInfoDialog dialog) {
                Message message = allHandler.obtainMessage();
                message.obj = bean;
                message.what = HandlerConstant.REGISTER_TYPE;
                allHandler.sendMessage(message);
                dialog.dismiss();
            }
        }).show();
    }

}
